package com.lyc.common.exception;


import org.springframework.core.annotation.Order;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.lyc.common.common.DepartCommonResult;

/**
 * @Author 李大神仙
 * @Date 2024/3/19 22:20
 * @Version 1.0
 */
@RestControllerAdvice
@Order(9999)
public class GlobalExceptionHandler {
   /* @ExceptionHandler({DepartException.class})
    public DepartCommonResult businessException(DepartException e){
        return DepartCommonResult.failed(e);
    }*/

   /* @ExceptionHandler({MethodArgumentNotValidException.class})
    public DepartCommonResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e){
        return handleBingingResult(e.getBindingResult());
    }*/

   /* @ExceptionHandler({IllegalArgumentException.class})
    public DepartCommonResult<Void> illegalArgumentException(IllegalArgumentException e){
        return DepartCommonResult.failed(e.getMessage());
    }*/

    /*private DepartCommonResult<String> handleBingingResult(BindingResult result){
        //把MethodArgumentNotValidException异常处理为对应的ApiRestResponse统一返回对象
        //这儿创建一个List集合，后面我们在MethodArgumentNotValidException中获取的错误信息，都存放在这个集合中去
        List<String> list = new ArrayList<>();
        //如果BindingResult中，包含错误，就获取其中所有的错误信息
        if (result.hasErrors()){
            List<ObjectError> allErrors = result.getAllErrors();
            //遍历所有的错误信息
            for (int i = 0; i < allErrors.size(); i++) {
                ObjectError objectError = allErrors.get(i);
                //提取具体的错误信息
                String message = objectError.getDefaultMessage();
                //将错误信息添加list集合中
                list.add(message);
            }
        }
        if (list.size() == 0){
            return DepartCommonResult.failed("请求参数异常");
        }
        //根据MethodArgumentNotValidException异常的具体错误信息，构建ApiResponse统一返回对象
        return DepartCommonResult.failed(list.toString());
    }*/
}
